/**
 * 
 */
package com.jm.web.form;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.tags.support.BaseTag;

import net.sf.json.JSONObject;

/**
 * @author yanjie
 * Nov 19, 2011 9:19:21 AM
 */
public class Paging {
	private int totalPage = 0;
	private int totalRows = 0;
	private int pageSize = 0;
	private int currentPage = 0; 
	private boolean bPagingInfo = false;
	private String width = "";
	private String gridid = "";
	private String pagingAlign = "";
	
	public String getPagingAlign() {
		return pagingAlign;
	}

	public void setPagingAlign(String pagingAlign) {
		this.pagingAlign = pagingAlign;
	}

	public String getGridid() {
		return gridid;
	}

	public void setGridid(String gridid) {
		this.gridid = gridid;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public boolean isPagingInfo() {
		return bPagingInfo;
	}

	public void setPagingInfo(boolean bPagingInfo) {
		this.bPagingInfo = bPagingInfo;
	}
	
	public void init(BaseTag tag){
		if(tag == null)return;
		this.setPagingInfo(tag.GetExtValue("pagingInfo", true));
		if(!tag.IsContainsKey("pagingWidth")){
			this.setWidth("100%");
		}else{
			this.setWidth(tag.GetExtValue("pagingWidth",""));
		}
		this.setPagingAlign(tag.GetExtValue("pagingAlign", "left"));
	}

	public int getBegin(){
		if(this.getTotalRows() == 0)
			return 0;
		
		return (this.getCurrentPage()-1) * this.getPageSize() +1;
	}
	
	public int getEnd(){
		if(this.getTotalRows() == 0)
			return 0;
		int end = this.getCurrentPage() * this.getPageSize();
		if(end > this.getTotalRows())
			end = this.getTotalRows();
		return end;
	}
	
	public String OutputHtmlCode(){
		StringBuilderEx sb = new StringBuilderEx();
		Separate sep = new Separate();
		
		sb.Reset();
		sb.Append("<table cellspacing=\"0\" border=\"0\" cellpadding=\"0\" class=\"listing-noborder\" width=\"%1$s\"><tbody><tr style=\"background-color: #D5E2F2;\">",this.getWidth());
		sb.Append("<td align=\"%1$s\" style=\"float:%1$s;\" nowrap=\"nowrap\">",this.getPagingAlign());
		sb.Append("<table cellspacing=\"0\" border=\"0\" class=\"listing-noborder\"><tbody><tr>");
		
		boolean bfirst = !(this.getCurrentPage() <= 1);
		sb.Append("<td style=\"padding-left:0px;padding-right:0px;\" class=\"%1$s\">",!bfirst ? "ax-item-disabled" : "");
		Button first = Toolbar.getNewButton("","paging_frist","paging_frist","ax-tbar-page-first xt-btn-icon",null);
		first.setOnClick(StringHelper.Format("doPaging(%1$s,%2$s,'%3$s');return false;",
				1,
				this.toJsonString().replaceAll("\"","'"),
				this.getGridid()
				));
		if(!bfirst){
			first.setEnab(false);
		}
		sb.Append(first.toButtonString());
		sb.Append("</td>");
		
		boolean bpre = !(this.getCurrentPage() <= 1);
		sb.Append("<td style=\"padding-left:0px;padding-right:0px;\" class=\"%1$s\">",!bpre ? "ax-item-disabled" : "");
		Button pre = Toolbar.getNewButton("","paging_prev","paging_prev","xt-btn-icon ax-tbar-page-prev",null);
		pre.setOnClick(StringHelper.Format("doPaging(%1$s,%2$s,'%3$s');return false;",
					this.getCurrentPage()-1,
					this.toJsonString().replaceAll("\"","'"),
					this.getGridid()
					));
		if(!bpre){
			pre.setEnab(false);
		}
		sb.Append(pre.toButtonString());
		sb.Append("</td>");
		
		sb.Append("<td style=\"padding-left:0px;padding-right:0px;\">");
		sb.Append(sep.toOutString());
		sb.Append("</td>");
		
		
		sb.Append("<td style=\"font: 12px tahoma,arial,helvetica,sans-serif;padding-left:0px;padding-right:0px;\">");
		sb.Append("當前 ");
		sb.Append("<input type=\"text\" name=\"pageing_page\" "
				+" onkeydown=\"if(event.keyCode==13)doPaging(this.value,%2$s,'%3$s');\" "
				+" realvalue=\"%1$s\" value=\"%1$s\" "
				+" style=\"font: 12px tahoma,arial,helvetica,sans-serif;height: 14px;line-height: 14px;vertical-align: middle;width: 20px;\"/>",
					this.getCurrentPage(),
					this.toJsonString().replaceAll("\"","'"),
					this.getGridid());
		sb.Append("/%1$s 頁",this.getTotalPage());
		sb.Append("</td>");
		
		sb.Append("<td style=\"padding-left:0px;padding-right:0px;\">");
		
		sb.Append(sep.toOutString());
		sb.Append("</td>");
		
		boolean bnext = this.getCurrentPage() < this.getTotalPage();
		sb.Append("<td style=\"padding-left:0px;padding-right:0px;\" class=\"%1$s\">",!bnext ? "ax-item-disabled" : "");
		Button next = Toolbar.getNewButton("","paging_next","paging_next","xt-btn-icon ax-tbar-page-next",null);
		next.setOnClick(StringHelper.Format("doPaging(%1$s,%2$s,'%3$s');return false;",
				this.getCurrentPage()+1,
				this.toJsonString().replaceAll("\"","'"),
				this.getGridid()
				));
		if(!bnext){
			next.setEnab(false);
		}
		sb.Append(next.toButtonString());
		sb.Append("</td>");
		
		boolean blast = this.getTotalPage() > 1 && this.getCurrentPage() != this.getTotalPage();
		sb.Append("<td style=\"padding-left:0px;padding-right:0px;\" class=\"%1$s\">",!blast ? "ax-item-disabled" : "");
		Button last = Toolbar.getNewButton("","paging_last","paging_last","xt-btn-icon ax-tbar-page-last",null);
		last.setOnClick(StringHelper.Format("doPaging(%1$s,%2$s,'%3$s');return false;",
				this.getTotalPage(),
				this.toJsonString().replaceAll("\"","'"),
				this.getGridid()));
		if(!blast){
			last.setEnab(false);
		}
		sb.Append(last.toButtonString());
		sb.Append("</td>");
		
		if(this.isPagingInfo()){
			sb.Append("<td style=\"padding-left:0px;padding-right:0px;\">");
			sb.Append(sep.toOutString());
			sb.Append("</td>");
			sb.Append("<td style=\"padding-left:0px;padding-right:0px;\">");
			sb.Append("當前顯示記錄 %1$s - %2$s 總計 %3$s",this.getBegin(),this.getEnd(),this.getTotalRows());
			sb.Append("</td>");
		}
		sb.Append("</tr></tbody></table>");
		
		sb.Append("</td>");
		sb.Append("</tr></tbody></table>");
		return sb.toString();
	}
	
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getTotalRows() {
		return totalRows;
	}
	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}
	
	public String toJsonString(){
		JSONObject json = JSONObject.fromBean(this);
		return json.toString();
	}
}
